package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrOctetString;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;

public class TransResult extends AxdrType {
    private ByteBuffer dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private AxdrEnum dar = null;
    private AxdrOctetString retResult = null;

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        DAR(0),
        RET_RESULT(1);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }

    public TransResult() {
    }

    public ByteBuffer getDataCode() {
        return dataCode;
    }

    public Choice getChoice() {
        return choice;
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public AxdrOctetString getRetResult() {
        return retResult;
    }

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        switch (this.choice) {
            case DAR:
                this.dar = new AxdrEnum();
                this.dar.setConst();
                codeLength += this.dar.decode(input);
                return codeLength;
            case RET_RESULT:
                this.retResult = new AxdrOctetString();
                codeLength += this.retResult.decode(input);
                return codeLength;
            default:
                throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
        }
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            byte[] bytes = dataCode.array();
            for (int i = bytes.length - 1; i >= 0; i--) {
                output.write(bytes[i]);
            }
            return bytes.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        switch (this.choice) {
            case RET_RESULT:
                codeLength += this.retResult.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            case DAR:
                codeLength += this.dar.encode(output);
                c = new AxdrEnum();
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                return codeLength;
            default:
                throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
        }
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.toByteArray());
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.dar = null;
        this.retResult = null;
    }

    public void setDar(AxdrEnum dar) {
        resetChoices();
        this.choice = Choice.DAR;
        this.dar = dar;
    }

    public void setRetResult(AxdrOctetString retResult) {
        resetChoices();
        this.choice = Choice.RET_RESULT;
        this.retResult = retResult;
    }

    @Override
    public String toString() {
        switch (this.choice) {
            case DAR:
                return this.dar != null ? "choice: {dar: " + this.dar + "}" : "choice is dar but dar is null";
            case RET_RESULT:
                return this.retResult != null ? "choice: {retresult: " + this.retResult + "}" : "choice is retResult but retResult is null";
            default:
                return "unknown";
        }
    }
}



